package com.mes.common.model;

import java.util.Calendar;
import java.util.Date;

public class MMTask {
	public static final String DEFAULT_LABEL = "默认";
	public static final int DEFAULT_VALUE = -1;

	public static final String MONDAY_LABEL = "星期一";
	public static final int MONDAY_VALUE = Calendar.MONDAY;
	public static final String TUESDAY_LABEL = "星期二";
	public static final int TUESDAY_VALUE = Calendar.TUESDAY;
	public static final String THURSDAY_LABEL = "星期三";
	public static final int THURSDAY_VALUE = Calendar.THURSDAY;
	public static final String WEDNESDAY_LABEL = "星期四";
	public static final int WEDNESDAY_VALUE = Calendar.WEDNESDAY;
	public static final String FRIDAY_LABEL = "星期五";
	public static final int FRIDAY_VALUE = Calendar.FRIDAY;
	public static final String SATURDAY_LABEL = "星期六";
	public static final int SATURDAY_VALUE = Calendar.SATURDAY;
	public static final String SUNDAY_LABEL = "星期日";
	public static final int SUNDAY_VALUE = Calendar.SUNDAY;

	public static final String JANUARY_LABEL = "一月";
	public static final int JANUARY_VALUE = Calendar.JANUARY;
	public static final String FEBRUARY_LABEL = "二月";
	public static final int FEBRUARY_VALUE = Calendar.FEBRUARY;
	public static final String MARCH_LABEL = "三月";
	public static final int MARCH_VALUE = Calendar.MARCH;
	public static final String APRIL_LABEL = "四月";
	public static final int APRIL_VALUE = Calendar.APRIL;
	public static final String MAY_LABEL = "五月";
	public static final int MAY_VALUE = Calendar.MAY;
	public static final String JUNE_LABEL = "六月";
	public static final int JUNE_VALUE = Calendar.JUNE;
	public static final String JULY_LABEL = "七月";
	public static final int JULY_VALUE = Calendar.JULY;
	public static final String AUGUST_LABEL = "八月";
	public static final int AUGUST_VALUE = Calendar.AUGUST;
	public static final String SEPTEMBER_LABEL = "九月";
	public static final int SEPTEMBER_VALUE = Calendar.SEPTEMBER;
	public static final String OCTOBER_LABEL = "十月";
	public static final int OCTOBER_VALUE = Calendar.OCTOBER;
	public static final String NOVEMBER_LABEL = "十一月";
	public static final int NOVEMBER_VALUE = Calendar.NOVEMBER;
	public static final String DECEMBER_LABEL = "十二月";
	public static final int DECEMBER_VALUE = Calendar.DECEMBER;

	public static final String LAST_DAY_OF_MONTH_LABEL = "月最后一天";
	public static final int LAST_DAY_OF_MONTH_VALUE = 0;

	public static final String HOUR_UNIT_LABEL = "小时";
	public static final int HOUR_UNIT_VALUE = 1;

	public static final String DAY_UNIT_LABEL = "日";
	public static final int DAY_UNIT_VALUE = 2;

	public static final String MONTH_UNIT_LABEL = "月";
	public static final int MONTH_UNIT_VALUE = 3;

	public static final String MINUTE_UNIT_LABEL = "分钟";
	public static final int MINUTE_UNIT_VALUE = 4;

	private String name;
	private Calendar startDate;
	private String state;
	private int year;
	private int month;
	private int dayOfMonth;
	private int week;
	private int dayOfWeek;
	private int hour;
	private int minute;
	private int second;
	private int periodValue;
	private int periodUnit;
	private String beanName;
	private String methodName;
	private boolean enable;
	private int runTimes;

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public void setStartDate(Date startDate) {
		Calendar calendar = Calendar.getInstance();
		if (startDate != null) {
			calendar.setTime(startDate);
		}
		this.startDate = calendar;
	}

	public int getRunTimes() {
		return runTimes;
	}

	public void setRunTimes(int runTimes) {
		this.runTimes = runTimes;
	}

	public MMTask(String _name) {
		name = _name;
		year = DEFAULT_VALUE;
		month = DEFAULT_VALUE;
		dayOfMonth = DEFAULT_VALUE;
		week = DEFAULT_VALUE;
		dayOfWeek = DEFAULT_VALUE;
		hour = DEFAULT_VALUE;
		minute = DEFAULT_VALUE;
		second = DEFAULT_VALUE;
		periodValue = 1;
		periodUnit = DAY_UNIT_VALUE;
		beanName = "";
		enable = false;
		runTimes = 0;
		state = "未调用";
	}

	public MMTask() {
		name = "";
		year = DEFAULT_VALUE;
		month = DEFAULT_VALUE;
		dayOfMonth = DEFAULT_VALUE;
		week = DEFAULT_VALUE;
		dayOfWeek = DEFAULT_VALUE;
		hour = DEFAULT_VALUE;
		minute = DEFAULT_VALUE;
		second = DEFAULT_VALUE;
		periodValue = 1;
		periodUnit = DAY_UNIT_VALUE;
		beanName = "";
		enable = false;
		runTimes = 0;
		state = "未调用";
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}
	//刷新任务的下一次执行时间
	public void refreshStartDate() {
		Calendar tempCalendar = Calendar.getInstance();
		startDate = Calendar.getInstance();
		if (year != DEFAULT_VALUE) {
			startDate.set(Calendar.YEAR, year);
		}
		if (month != DEFAULT_VALUE) {
			startDate.set(Calendar.MONTH, month);
		}
		if (dayOfMonth != DEFAULT_VALUE) {
			if (dayOfMonth == LAST_DAY_OF_MONTH_VALUE) {
				int temp = startDate.getActualMaximum(Calendar.DAY_OF_MONTH);
				startDate.set(Calendar.DAY_OF_MONTH, temp);
			} else {
				startDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			}
		}
		if (week != DEFAULT_VALUE) {
			startDate.set(Calendar.WEEK_OF_YEAR, week);
		}
		if (dayOfWeek != DEFAULT_VALUE) {
			startDate.set(Calendar.DAY_OF_WEEK, dayOfWeek);
		}
		if (hour != DEFAULT_VALUE) {
			startDate.set(Calendar.HOUR_OF_DAY, hour);
		}
		if (minute != DEFAULT_VALUE) {
			startDate.set(Calendar.MINUTE, minute);
		}
		if (second != DEFAULT_VALUE) {
			startDate.set(Calendar.SECOND, second);
		}
		while (tempCalendar.getTimeInMillis() >= startDate.getTimeInMillis()) {
			if (periodUnit == HOUR_UNIT_VALUE) {
				long period = periodValue * 60L * 60L * 1000L
						+ startDate.getTimeInMillis();
				startDate.setTimeInMillis(period);
			} else if (periodUnit == DAY_UNIT_VALUE) {
				long period = periodValue * 24L * 60L * 60L * 1000L
						+ startDate.getTimeInMillis();
				startDate.setTimeInMillis(period);
			} else if (periodUnit == MONTH_UNIT_VALUE) {
				startDate.add(Calendar.MONTH, periodValue);
				if (dayOfMonth != DEFAULT_VALUE) {
					if (dayOfMonth == LAST_DAY_OF_MONTH_VALUE) {
						int temp = startDate
								.getActualMaximum(Calendar.DAY_OF_MONTH);
						startDate.set(Calendar.DAY_OF_MONTH, temp);
					} else {
						startDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
					}
				} else {
					startDate.set(Calendar.DAY_OF_MONTH, tempCalendar
							.get(Calendar.DAY_OF_MONTH));
				}
			} else if (periodUnit == MINUTE_UNIT_VALUE) {
				long period = periodValue * 60L * 1000L
						+ startDate.getTimeInMillis();
				startDate.setTimeInMillis(period);
			}
		}
	}

	public Date getStartDate() {
		Date date = null;
		if (startDate != null) {
			date = startDate.getTime();
		}
		return date;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDayOfMonth() {
		return dayOfMonth;
	}

	public void setDayOfMonth(int dayOfMonth) {
		this.dayOfMonth = dayOfMonth;
	}

	public int getWeek() {
		return week;
	}

	public void setWeek(int week) {
		this.week = week;
	}

	public int getDayOfWeek() {
		return dayOfWeek;
	}

	public void setDayOfWeek(int dayOfWeek) {
		this.dayOfWeek = dayOfWeek;
	}

	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public int getMinute() {
		return minute;

	}

	public void setMinute(int minute) {
		this.minute = minute;
	}

	public int getSecond() {
		return second;
	}

	public void setSecond(int second) {
		this.second = second;
	}

	public int getPeriodValue() {
		return periodValue;
	}

	public void setPeriodValue(int periodValue) {
		this.periodValue = periodValue;
	}

	public int getPeriodUnit() {
		return periodUnit;
	}

	public void setPeriodUnit(int periodUnit) {
		this.periodUnit = periodUnit;
	}

	public long getPeriod() {
		long period = 0L;
		if (periodUnit == HOUR_UNIT_VALUE) {
			period = periodValue * 60L * 60L * 1000L;
		} else if (periodUnit == DAY_UNIT_VALUE) {
			period = periodValue * 24L * 60L * 60L * 1000L;
		}
		return period;
	}

	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public boolean equals(Object obj) {
		if (this.getName().equals(((MMTask) obj).getName())) {
			return true;
		} else {
			return false;
		}
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

}
